# -*- coding:utf-8 -*-
import MySQLdb
from MySQLdb.cursors import DictCursor, BaseCursor
conn=MySQLdb.connect(host="127.0.0.1",port=3306,user="root",passwd="FeDcQ7wHdXUG",db="erm",charset="utf8")#连接数据库
cursor = conn.cursor()#获取游标

class ermdb:
    # ---初始化数据库连接
    def __init__(self, dict=""):
        self.cursor = cursor
        self.conn = conn

    # ----更新到数据库
    def updatetodb(self, sql, argument=''):
        if argument:
            self.cursor.execute(sql, argument)
        else:
            self.cursor.execute(sql)
        # self.cursor.execute(sql, argument)
        self.cursor.execute('SELECT last_insert_id() as lastid')
        result = self.cursor.fetchone()
        self.conn.commit()
        if result:
            return result

    # ----查询所有数据
    def fetchalldb(self, sql, argument=''):
        if argument:
            self.cursor.execute(sql, argument)
        else:
            self.cursor.execute(sql)
        resultlist = self.cursor.fetchall()
        if resultlist:
            return resultlist
        else:
            return []

    # ----查询一条数据
    def fetchonedb(self, sql, argument=''):
        if argument:
            self.cursor.execute(sql, argument)
        else:
            self.cursor.execute(sql)
        result = self.cursor.fetchone()
        if result:
            return result

    # ----查询所有数据(主数据库)
    def fetchalldbmain(self, sql, argument=''):
        if argument:
            self.cursor.execute(sql, argument)
        else:
            self.cursor.execute(sql)
        resultlist = self.cursor_comp.fetchall()
        if resultlist:
            return resultlist
        else:
            return []

    # ----查询一条数据(主数据库)
    def fetchonedbmain(self, sql, argument=''):
        if argument:
            self.cursor.execute(sql, argument)
        else:
            self.cursor.execute(sql)
        result = self.cursor_comp.fetchone()
        if result:
            return result

    # ----查询一条总数
    def fetchnumberdb(self, sql, argument=''):
        if argument:
            self.cursor.execute(sql, argument)
        else:
            self.cursor.execute(sql)
        result = self.cursor.fetchone()
        if result:
            return result[0]
        else:
            return 0

    # ----关闭连接
    def closedb(self):
        self.cursor.close()

    # ----数据事物回滚
    def rollbackdb(self):
        self.cursor.rollback()
